package com.finance.system.test;

import java.util.LinkedList;

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

        ListNode ls = null;

        int increase = 0;//初始进位值
        while(l1 != null || l2 != null || increase > 0){
            int sum1 = l1 == null ?  0 : l1.val;
            int sum2 = l2 == null ?  0 : l2.val;

            int sum12m = (sum1 + sum2 + increase)/10;//进位
            int sum12y = (sum1 + sum2 + increase)%10;//和

            increase = sum12m;//设置进位



            ListNode node = new ListNode(sum12y) ;//构造节点保存结果

            if(ls == null){
                ls = node;
            }else{
                ListNode last = ls;
                while(last.next != null){
                    last = last.next;
                }
                last.next = node;
            }
            if(l1 != null)  l1 = l1.next;
            if(l2 != null)  l2 = l2.next;

        }

        return ls;
    }

}
